$(function(){
})
function getHistoryAddress(typejsp){
	var jsonstr = "filterscount=0&groupscount=0&pagenum=0&pagesize=10&recordstartindex=0&recordendindex=10&iphone="+getCookie("useriphone")+"&addressLike=";
	$.ajax({
		cache: false,
		type: "post",
		datatype: 'json',
		contentType: 'application/json;charset=UTF-8',
		data: jsonstr,
		url: ''+httpipstr+'alfa-mobile-ws/rest/historyaddress/findlist?mobiletoken='+getCookie("mobiletokenuser")+'',
		success: function(data) {
			var str="";
			if(data.total=="0"){
				if(typejsp=="0"){
					$(".people").find("span").html("暂无收油地址请添加");
				}else if(typejsp=="1"){
					layui.use('layer', function(){
						layer.load(2, {time: 1*1000})
					})
					var str="<li>暂无收油地址请添加</li>"
						$(".content").html(str)
				}
			}else{
				if(typejsp=="0"){
					if(getCookie("recycleaddress")==""){
						setcookie("recycleaddress", data.rows[0].address, 30, "/");
						setcookie("recycleaddressid", data.rows[0].id, 30, "/");
					}else{

					}
					$(".people").find("span").html(""+getCookie("recycleaddress")+"");
				}else if(typejsp=="1"){
					layui.use('layer', function(){
						layer.load(2, {time: 1*1000})
					})
					$.each(data.rows, function (n, value) {
						if(getCookie("recycleaddressid")==value.id){
							str+='<li id="'+value.id+'" name="'+value.address+'" class="addressli">'
							+'<div class="address">'
							+'<div class="name">'
							+'<h2 style="font-size: 0.35rem;">'+getCookie("realname")+'</h2>'
							+'<h3 style="font-size: 0.35rem;">'+getCookie("useriphone")+'</h3>'
							+'</div>'
							+'<h4 style="font-size: 0.3rem;">地址:'+value.address+'</h4>'
							+'<div class="edit">'
							+'<p>'
							+'<input data-shebao="suibianyige" type="checkbox" checked />'
							+'<span class="mo">默认地址</span>'
							+'</p>'
							+'<p>'
							+'<a onclick="longPress('+value.id+')">'
							+'<i class="icon iconfont icon-empty"></i>'
							+'<span>删除</span>'
							+'</a>'
							+'</p>'
							+'</div>'
							+'</div>'
							+'</li>'
						}else{
							str+='<li id="'+value.id+'" name="'+value.address+'" class="addressli">'
							+'<div class="address">'
							+'<div class="name">'
							+'<h2 style="font-size: 0.35rem;">'+getCookie("realname")+'</h2>'
							+'<h3 style="font-size: 0.35rem;">'+getCookie("useriphone")+'</h3>'
							+'</div>'
							+'<h4 style="font-size: 0.3rem;">地址:'+value.address+'</h4>'
							+'<div class="edit">'
							+'<p>'
							+'<input data-shebao="suibianyige" type="checkbox"  />'
							+'<span class="mo">默认地址</span>'
							+'</p>'
							+'<p>'
							+'<a onclick="longPress('+value.id+')">'
							+'<i class="icon iconfont icon-empty"></i>'
							+'<span>删除</span>'
							+'</a>'
							+'</p>'
							+'</div>'
							+'</div>'
							+'</li>'
						}

					})
					$(".content").html(str);
					 onclickaddress();
					if($("#defaultaddress").prop("checked")){
						$(".addressli").find(".name").eq(0).click();
					}
					$("input[data-shebao=suibianyige]").on("click", function() {
						$("input[data-shebao=suibianyige]").prop("checked", false);
						$(this).prop("checked", true);
						$(this).parent().parent().parent().parent("li").click();
						//$(this).parent().find(".mo").css("color","#ff5500");
					});
					
				}
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			layer.msg('账号已在其他地方登陆请重新登录');
			clearuserCookieWithIdAll();
			location.href="../weixinuser/login.jsp";
			}
	})
}
/**
 * 添加收油地址
 * @returns
 */
function isInteger(obj) {  return Math.floor(obj) === obj } 
function insertorders(){
	var remark=$("#textarea").val();
	var num=$("#num").val();
	var iphone=$("#iphone").val();
	var address=getCookie("recycleaddress");
	var iphonetype=true;
	var addresstype=true;
	var index = layer.load(2, {
		shade: [0.1, '#fff'],
		time: 3000
	});
	if(num.toString().length>5 ){
		iphonetype=false;
		layer.msg('请输入有效的桶数');
		/*tippopup("#num","请输入有效的桶数")*/
	}
	 if(!num.match(/^\+?[1-9][0-9]*$/ )||num=="0"){
		iphonetype=false;
		layer.msg('请输入有效的桶数');
		/*tippopup("#num","请输入有效的桶数")*/
	}
	 if(iphone==""){
		iphonetype=false;
		layer.msg('请输入联系方式');
		/*tippopup("#iphone","请输入联系方式")*/
	} if(address==""){
		addresstype=false;
		layer.msg('请添加地址');
		/*tippopup(".address","请添加地址")*/
	} if(!iphone.match(/^1[34578]\d{9}$/)) {
		iphonetype=false;
		layer.msg('输入有效的手机号');
		/* tippopup("#iphone","输入有效的手机号")*/
	}
	else if(iphonetype&&addresstype){
		var json={"username":""+getCookie("realname")+"","iphone":""+iphone+"","address":""+address+"","num":""+num+"","orgname":""+getCookie("orgname")+"","orgstatus":"1","remark":""+(!remark ||remark=="undefined"? "" : ""+remark+"")+"","isSms":"0"};
		$.ajax({
			type: "post",
			datatype: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: ''+httpipstr+'alfa-mobile-ws/rest/order/insertorders?mobiletoken='+getCookie("mobiletokenuser")+'',
			data: JSON.stringify(json),
			success: function(data) {
				if(data.message=="1"){
					var json1 = {
							"openid": ""+getCookie("openid")+"",
						}
					$.ajax({
						type: "post",
						datatype: 'json',
						contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
						url: '../recycle/TextToUser',
						data: JSON.stringify(json1),
						success: function(data) {
							if(data.code=="0"){
								layer.closeAll();
								location.href="../weixinuser/information.jsp"
							}
						}
					})
					
				}else if(data.message=="2"){
					layer.msg('插入订单失败，请重试');
					/* tippopup("#insertorders","插入订单失败，请重试")*/
				}else if(data.message=="3"){
					layer.msg("当天的订单总数超出最大数量限制，请明天再发起收油")
					/*tippopup("#insertorders","当天的订单总数超出最大数量限制，请明天再发起收油")*/
				}
				
			}})
	}

}
